#include <func.h>

#define N 10000000
typedef struct{
    int num;
    pthread_mutex_t mutex;
}data_t;

void* thread_func(void* p)
{
    int i;
    data_t* d=(data_t*)p;
    for(i=0;i<N;i++)
    {
        pthread_mutex_lock(&d->mutex);
        d->num+=1;
        pthread_mutex_unlock(&d->mutex);
    }
}

int main()
{
    int ret;
    data_t d;
    d.num=0;//初始化锁
    pthread_mutex_init(&d.mutex,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_mutex_init")
    pthread_t pthid;
    ret=pthread_create(&pthid,NULL,thread_func,(void*)&d);
    THREAD_ERROR_CHECK(ret,"pthread_create")
    int i;
    for(i=0;i<N;i++)
    {
        pthread_mutex_lock(&d.mutex);
        d.num+=1;
        pthread_mutex_unlock(&d.mutex);
    }
    ret=pthread_join(pthid,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_join")
    printf("reasult=%d\n",d.num);
    ret=pthread_mutex_destroy(&d.mutex);//销毁锁
    THREAD_ERROR_CHECK(ret,"pthread_mutex_destroy")
    return 0;
}
